package bll

import (
	"context"
	"maya-service/model/po"
	"maya-service/store/mysql"
	"maya-service/utils"

	"maya-service/model"
	"maya-service/model/entity"
	"maya-service/model/mapping"
	"maya-service/store"
	"time"
)

type aiQuestionConsultStatics struct {
	iAiQuestionConsultStatics store.IAiQuestionConsultStatics
}

var AiQuestionConsultStatics = &aiQuestionConsultStatics{
	iAiQuestionConsultStatics: mysql.AiQuestionConsultStatics,
}

func init() {
	Register(AiQuestionConsultStatics)
}

func (a *aiQuestionConsultStatics) init() func() {
	return func() {}
}

// Statics 统计
func (a *aiQuestionConsultStatics) Statics(ctx context.Context, in *po.AiQuestionConsultStaticsReq) {
	// 题目信息
	questionInfo, err := AiQuestion.Find(ctx, &model.AiQuestionInfoRequest{Id: in.QuestionId})
	if err != nil {
		return
	}

	findParams := &model.AiQuestionConsultStaticsInfoRequest{
		QuestionId: in.QuestionId,
	}

	// 结果
	find, _ := a.Find(ctx, findParams)
	if find != nil && find.Id > 0 {
		find.Count += 1
		avg := utils.RoundToTwoDecimalPlaces(find.Avg*float64(find.Count-1) + float64(in.Score)/float64(find.Count))
		_ = a.Update(ctx, &model.AiQuestionConsultStaticsUpdateRequest{
			Id:    find.Id,
			Avg:   &avg,
			Count: &find.Count,
		})
		return
	}

	createData := &model.AiQuestionConsultStaticsCreateRequest{
		QuestionId:    in.QuestionId,
		QuestionTopic: questionInfo.Topic,
		Questioner:    questionInfo.Questioner,
		Avg:           float64(in.Score),
		Count:         1,
	}

	_ = a.Create(ctx, createData)
	return
}

// Create
func (a *aiQuestionConsultStatics) Create(ctx context.Context, in *model.AiQuestionConsultStaticsCreateRequest) error {
	var (
		err error
	)
	c := buildAiQuestionConsultStatics(in)
	_, err = a.iAiQuestionConsultStatics.Create(ctx, c)
	return err
}

// Update
func (a *aiQuestionConsultStatics) Update(ctx context.Context, in *model.AiQuestionConsultStaticsUpdateRequest) error {
	var (
		dict = make(map[string]interface{})
	)

	if in.QuestionId != nil {
		dict["question_id"] = in.QuestionId
	}

	if in.QuestionTopic != nil {
		dict["question_topic"] = in.QuestionTopic
	}

	if in.Avg != nil {
		dict["avg"] = in.Avg
	}

	if in.Count != nil {
		dict["count"] = in.Count
	}

	if in.RankType != nil {
		dict["rank_type"] = in.RankType
	}

	if in.CreatedAt != nil {
		dict["created_at"] = in.CreatedAt
	}

	if in.UpdatedAt != nil {
		dict["updated_at"] = in.UpdatedAt
	}

	// do other update here
	updateAt := time.Now().Unix()
	in.UpdatedAt = &updateAt
	return a.iAiQuestionConsultStatics.Update(ctx, in.Id, dict)
}

// Delete
func (a *aiQuestionConsultStatics) Delete(ctx context.Context, in *model.AiQuestionConsultStaticsDeleteRequest) error {
	return a.iAiQuestionConsultStatics.Delete(ctx, in.Id)
}

// List
func (a *aiQuestionConsultStatics) List(ctx context.Context, in *model.AiQuestionConsultStaticsListRequest) (*model.AiQuestionConsultStaticsListResponse, error) {
	var (
		err   error
		total int
		list  []*entity.AiQuestionConsultStatics
		out   = &model.AiQuestionConsultStaticsListResponse{}
	)

	if total, list, err = a.iAiQuestionConsultStatics.List(ctx, in); err != nil {
		return nil, err
	}

	out.Total = total
	out.List = mapping.AiQuestionConsultStaticssEntityToDto(list)

	return out, nil
}

// Find
func (a *aiQuestionConsultStatics) Find(ctx context.Context, in *model.AiQuestionConsultStaticsInfoRequest) (*model.AiQuestionConsultStaticsInfo, error) {
	var (
		err  error
		data *entity.AiQuestionConsultStatics
		out  = &model.AiQuestionConsultStaticsInfo{}
	)

	if data, err = a.iAiQuestionConsultStatics.Find(ctx, in); err != nil {
		return nil, err
	}

	out = mapping.AiQuestionConsultStaticsEntityToDto(data)
	return out, nil
}

// buildAiQuestionConsultStatics build entity
func buildAiQuestionConsultStatics(in *model.AiQuestionConsultStaticsCreateRequest) *entity.AiQuestionConsultStatics {

	ety := &entity.AiQuestionConsultStatics{

		Questioner: "",
	}

	ety.QuestionId = in.QuestionId

	ety.QuestionTopic = in.QuestionTopic

	ety.Questioner = in.Questioner

	ety.Count = in.Count

	ety.Avg = in.Avg

	ety.RankType = in.RankType

	ety.CreatedAt = time.Now().Unix()

	ety.UpdatedAt = in.UpdatedAt

	return ety
}
